package apis

import (
	"fmt"
	"strings"
	"vrcm/src/vrcm/app/admin/models/live_schedule"
	"vrcm/src/vrcm/app/admin/service/dto/live_schedule"
	"vrcm/src/vrcm/app/admin/service/live_schedule"
	"vrcm/src/vrcm/common/apis"

	"github.com/gin-gonic/gin"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"vrcm/src/vrcm/common/actions"
)

type SourceGroup struct {
	apis.Api
}

// GetPage 获取SourceGroup列表
// @Summary 获取SourceGroup列表
// @Description 获取SourceGroup列表
// @Tags SourceGroup
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.SourceGroup}} "{"code": 200, "data": [...]}"
// @Router /api/v1/source-group [get]
// @Security Bearer
func (e SourceGroup) GetPage(c *gin.Context) {
	req := dto.SourceGroupGetPageReq{}
	s := service.SourceGroupService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]live_schedule.SourceGroup, 0, 5)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取SourceGroup失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取SourceGroup
// @Summary 获取SourceGroup
// @Description 获取SourceGroup
// @Tags SourceGroup
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.SourceGroup} "{"code": 200, "data": [...]}"
// @Router /api/v1/source-group/{id} [get]
// @Security Bearer
func (e SourceGroup) Get(c *gin.Context) {
	req := dto.SourceGroupGetReq{}
	s := service.SourceGroupService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var object live_schedule.SourceGroup

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取SourceGroup失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建SourceGroup
// @Summary 创建SourceGroup
// @Description 创建SourceGroup
// @Tags SourceGroup
// @Accept application/json
// @Product application/json
// @Param data body dto.SourceGroupInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/source-group [post]
// @Security Bearer
func (e SourceGroup) Insert(c *gin.Context) {
	req := dto.SourceGroupInsertReq{}
	s := service.SourceGroupService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			e.Error(apis.UpdateSourceError.Code, err, fmt.Sprintf("创建SourceGroup失败 %s", "信源组不能重复"))
		} else {
			e.Error(apis.AddSourceGroupError.Code, err, fmt.Sprintf("创建SourceGroup失败 %s", err.Error()))
		}
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改SourceGroup
// @Summary 修改SourceGroup
// @Description 修改SourceGroup
// @Tags SourceGroup
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.SourceGroupUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/source-group/{id} [put]
// @Security Bearer
func (e SourceGroup) Update(c *gin.Context) {
	req := dto.SourceGroupUpdateReq{}
	s := service.SourceGroupService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.Update(&req)
	if err != nil {
		e.Error(apis.UpdateSourceGroupError.Code, err, fmt.Sprintf("修改SourceGroup失败, %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除SourceGroup
// @Summary 删除SourceGroup
// @Description 删除SourceGroup
// @Tags SourceGroup
// @Param data body dto.SourceGroupDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/source-group [delete]
// @Security Bearer
func (e SourceGroup) Delete(c *gin.Context) {
	s := service.SourceGroupService{}
	req := dto.SourceGroupDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DeleteSourceGroupError.Code, err, fmt.Sprintf("删除SourceGroup失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e SourceGroup) SourceGroupRelation(c *gin.Context) {
	s := service.SourceGroupService{}
	req := dto.SourceGroupRelationReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SourceGroupRelation(&req)
	if err != nil {
		e.Error(apis.AddSourceGroupError.Code, err, fmt.Sprintf("修改信源组失败， %s", err.Error()))
		return
	}
	e.OK("", "修改信源组关系成功")
}

func (e SourceGroup) GetDetail(c *gin.Context) {
	req := dto.SourceGetDetailReq{}
	s := service.SourceGroupService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var count int64
	data, err := s.GetDetail(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取SourceGroup失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(data, int(count), req.Pagination.GetPageIndex(), req.Pagination.GetPageSize(), "查询成功")
}
